﻿
Partial Class App_Controls_DataPager
    Inherits MyUI.UserControlBaseApp




    Public Event PageChanged()


    Private WithEvents g As Control

    Property PageSize As Integer
    Property Controlid As String
    Public Property PageIndex() As Integer
        Get
            Return ViewState("PageIndex")

        End Get
        Set(ByVal value As Integer)
            ViewState("PageIndex") = value
        End Set
    End Property
    Public Property SortField() As String
        Get
            Return Utils.GetValue(ViewState, "SortField")
        End Get
        Set(value As String)
            ViewState("SortField") = value
        End Set
    End Property
    Public Property SortType() As WebControls.SortDirection
        Get
            Return ViewState("SortType")
        End Get
        Set(value As WebControls.SortDirection)
            ViewState("SortType") = value
        End Set
    End Property

    Public Property CssActive() As String
        Get
            Return ViewState("CssActive")
        End Get
        Set(value As String)
            ViewState("CssActive") = value
        End Set
    End Property
    Public Property CssNotActive() As String
        Get
            Return ViewState("CssNotActive")
        End Get
        Set(value As String)
            ViewState("CssNotActive") = value
        End Set
    End Property

    'Public Property DefSort() As String
    '    Get
    '        Return ViewState("DefSort")
    '    End Get
    '    Set(value As String)
    '        ViewState("DefSort") = value
    '    End Set
    'End Property


    Private Property PageCount As Integer = 0

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init

        g = FC(Of Control)(Me.Controlid, Me.Page) '    UI.Controls.FindControl(Me.Controlid, Me.Page.Controls)


        If g.GetType = GetType(ListView) Then

            AddHandler DirectCast(g, ListView).Sorting, AddressOf ListAccountConn_Sorting

        End If

        If Not Me.IsPostBack Then
            'If Me.DefSort <> "" Then
            '    Me.Sort(Me.DefSort)
            'End If

        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then
            If Me.SortField <> "" Then
                Me.Uisort(Me.SortField)
            End If

        End If

    End Sub



    Private Sub Uisort(sort As String)

        Dim linksort = UI.Controls.FindCommandControls(Of LinkButton)(g.Controls, "sort")

        For Each link In linksort
            If link.CommandArgument = sort Then
                UI.Controls.ClassReplace(link, Me.CssNotActive, Me.CssActive)
            Else
                UI.Controls.ClassReplace(link, Me.CssActive, Me.CssNotActive)

            End If
        Next

    End Sub


    Protected Sub ListAccountConn_Sorting(sender As Object, e As ListViewSortEventArgs)

        Me.Uisort(e.SortExpression)
        Me.Sort(e.SortExpression)

        RaiseEvent PageChanged()


    End Sub

    Private Sub SetListPager()
        Dim PagVis = 7
        Dim FirtPage = 0
        Dim LastPage = 0



        Me.LblPAges.Text = PageCount

        Dim data As New List(Of Object)

        If PageIndex > 0 Then
            data.Add(New With {.num = PageIndex - 1, .text = "&lt; prev", .class = "prev"})
        End If


        If (PagVis + 1) / 2 < PageIndex + 1 Then
            FirtPage = PageIndex + 1 - ((PagVis + 1) / 2)
            'data.Add(New With {.num = 0, .text = "&lt; prev", .class = "prev"})
        End If

        LastPage = FirtPage + PagVis - 1

        If LastPage > PageCount - 1 Then
            LastPage = PageCount - 1
            FirtPage = LastPage - PagVis + 1
        End If

        If FirtPage < 0 Then
            FirtPage = 0
        End If

        For ct = FirtPage To LastPage
            data.Add(New With {.num = ct, .text = ct + 1, .class = IIf(PageIndex = ct, "active", "p-n")})
        Next

        If PageIndex < PageCount - 1 Then
            data.Add(New With {.num = PageIndex + 1, .text = "next &gt", .class = "next"})
        End If

        Me.rep.DataSource = data
        Me.rep.DataBind()



    End Sub


    Private Function SortTypeToSql(st As WebControls.SortDirection) As String

        If st = SortDirection.Ascending Then
            Return "ASC"
        Else
            Return "DESC"
        End If

    End Function

    Public Sub Sort(Field As String)


        If Me.SortField.ToUpper = Field.ToUpper Then
            If Me.SortType = WebControls.SortDirection.Ascending Then
                Me.SortType = WebControls.SortDirection.Descending
            Else
                Me.SortType = WebControls.SortDirection.Ascending
            End If
        Else
            Me.SortType = WebControls.SortDirection.Ascending
        End If

        Me.SortField = Field


    End Sub

    Public Function SortQuery(Of tResult As {New, Class})(q As DBase.MySql.SqlQuery(Of tResult)) As DBase.MySql.SqlQuery(Of tResult)

        If Me.SortField = "" Then
            Return q
        Else
            Return q.SetOrderBy(Me.SortField & " " & SortTypeToSql(Me.SortType))
        End If


    End Function
    Protected Sub rep_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles rep.ItemCommand


        Me.PageIndex = e.CommandArgument
        'Me.g.DataBind()
        SetListPager()

        RaiseEvent PageChanged()

        'If g.GetType Is GetType(GridView) Then
        '    g.SetPageIndex(e.CommandArgument)
        'End If


    End Sub



    Protected Sub g_DataBinding(sender As Object, e As EventArgs) Handles g.DataBinding


        'Dim dt As DataTable = sender.datasource

        If sender.datasource IsNot Nothing Then
            Dim d As IList '= sender.datasource.DefaultView
            If sender.datasource.GetType = GetType(DataTable) Then
                d = sender.datasource.DefaultView
            Else
                d = sender.datasource
            End If

            Me.PageCount = Math.Ceiling(d.Count / Me.PageSize)

            sender.datasource = (From dr In d Skip Me.PageIndex * Me.PageSize Take Me.PageSize).ToList
        End If

        SetListPager()



    End Sub



End Class
